function lobby.send(name, msg)
   local map = lobby.game[name]
   for _, player in pairs(minetest.get_connected_players()) do
      local rname  = player:get_player_name()
      if lobby.game[rname] == map then
         minetest.chat_send_player(rname, "<"..name.."> "..msg)
      end
   end
   minetest.log("action", "CHAT: ".."<"..name.."> "..msg)
   -- Prevent from sending normally
   return true
end

function lobby.message_to_level(map_id, message)
   for _, player in pairs(minetest.get_connected_players()) do
      local rname  = player:get_player_name()
      if lobby.game[rname] == map_id then
         minetest.chat_send_player(rname, message)
      end
   end
end

minetest.register_on_chat_message(function(name, msg)
   return lobby.send(name, msg)
end)

minetest.register_chatcommand('survivors', {
   description = 'See what players are still on the level',
   func = function(name)
      local map_id = lobby.game[name]
      local survivors = lobby.players_on_level(map_id)
      minetest.chat_send_player(name, survivors..' are left on this map.')
   end
})

minetest.register_chatcommand('spawn', {
   description = 'Teleport to 0,0,0. (Builders only)',
   privs = {creative=true},
   func = function (name)
      local player = minetest.get_player_by_name(name)
      player:setpos({x=0, y=0, z=0})
   end
})

minetest.register_chatcommand('msg', {
   description = 'prevents people from sending private messages.',
   func = function(name)
      minetest.chat_send_player(name, 'Private messages are disabled here.')
   end
})

minetest.register_chatcommand('lobby', {
   description = 'Travel back to the lobby when a ghost.',
   func = function(name)
      local player = minetest.get_player_by_name(name)
      local ghost = player:get_attribute('ghost')
      if ghost == 'true' then
         gamer.player_set_textures(player,{'gamer_skin.png'})
         player:set_nametag_attributes({
            color = {a = 255, r = 255, g = 255, b = 255}
         })
         player:set_properties({visual_size = {x = 1, y = 1}, collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}})
         player:setpos({x=0, y=0, z=0})
         minetest.chat_send_player(name, 'Here you are.')
         player:set_attribute('ghost', 'false')
      else
         minetest.chat_send_player(name, 'You can only run this command when you are a ghost.')
      end
   end
})

minetest.register_chatcommand('kick', {
   description = 'Kick somebody, hopefully the traitor, from the level.',
   func = function(name, params)
      local map_id = lobby.game[name]
      if map_id ~= 'lobby' then
         local survivors = lobby.players_on_level(map_id)
         local actual_votes = lobby.votes[map_id] or 0
         if not lobby.voted[name] then
            lobby.vote_timer[map_id] = 1800
            minetest.register_globalstep(function(dtime)
               if lobby.vote_timer[map_id] == 0 then
                  lobby.vote_timer[map_id] = lobby.vote_timer[map_id] - 1
                  lobby.message_to_level(map_id, 'Voting time is over.')
                  lobby.vote(map_id, true)
               elseif lobby.vote_timer[map_id] == 300 then
                  lobby.vote_timer[map_id] = lobby.vote_timer[map_id] - 1
                  lobby.message_to_level(map_id, 'Thirty seconds left to vote.')
               elseif lobby.vote_timer[map_id] == 600 then
                  lobby.vote_timer[map_id] = lobby.vote_timer[map_id] - 1
                  lobby.message_to_level(map_id, 'One minute left to vote.')
               elseif lobby.vote_timer[map_id] == 1200 then
                  lobby.vote_timer[map_id] = lobby.vote_timer[map_id] - 1
                  lobby.message_to_level(map_id, 'Two minutes left to vote')
               end
            end)
            if params == 'skip' then --players opts to not vote
               minetest.chat_send_player(name, 'You skipped voting this round.')
               lobby.voted[name] = true
               lobby.votes[map_id] = actual_votes + 1
               lobby.vote(map_id)
            elseif params ~= '' then
               if string.find(survivors, params) then --player inputs a player name
                  minetest.chat_send_player(name, 'Thanks for your vote.')
                  lobby.voted[name] = true
                  lobby.votes[map_id] = actual_votes + 1
                  lobby.suspect[params] = lobby.suspect[params] + 1
                  lobby.vote(map_id)
               else --player votes for an invalid username
                  minetest.chat_send_player(name, 'Check your spelling and try again.')
               end
            end
         else
            minetest.chat_send_player(name, 'You can\'t vote right now.')
         end
      end
   end
})
